/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class IPCC_EXTENSION_GROUPS implements Serializable,Cloneable{
    private String group;
    private String group_name;
    private PARTNER partner_id = new PARTNER();

    public IPCC_EXTENSION_GROUPS() {        
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getGroup_name() {
        return group_name;
    }

    public void setGroup_name(String group_name) {
        this.group_name = group_name;
    }

    public PARTNER getPartner_id() {
        return partner_id;
    }

    public void setPartner_id(PARTNER partner_id) {
        this.partner_id = partner_id;
    }

    public static List<IPCC_EXTENSION_GROUPS> getAll(String condition) throws Exception{
        List<IPCC_EXTENSION_GROUPS> list = new ArrayList<IPCC_EXTENSION_GROUPS>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "SELECT group_,group_name,partner_id FROM ipcc_extension_groups "
                + " " + condition
                + " ORDER BY GROUP_NAME";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                IPCC_EXTENSION_GROUPS ipg = new IPCC_EXTENSION_GROUPS();
                ipg.setGroup(rs.getString(1));
                ipg.setGroup_name(rs.getString(2));
                PARTNER p = new PARTNER();
                p.setPartner_id(Integer.parseInt(rs.getString(3)));
                ipg.setPartner_id(p);
                list.add(ipg);
            }
            return list;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }
    
    public String getNameById() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String strSQL = " SELECT group_name FROM ipcc_extension_groups "
                + " WHERE group_ = ? ";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            pst.setString(1, getGroup());
            
            rs = pst.executeQuery();
            if (rs.next()) {
                return rs.getString(1);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
        return "";
    }
    public static String getIdBySource_number(String source_number) throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String strSQL = " select a.group_ from ipcc_extension_groups a,ipcc_group_channel b"
                + " where a.group_ = b.group_ and b.source_number = ? ";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            pst.setString(1, source_number);
            
            rs = pst.executeQuery();
            if (rs.next()) {
                return rs.getString(1);
            }
            return "";
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    public String getIdByName() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String strSQL = " SELECT group_ FROM ipcc_extension_groups "
                + " WHERE group_name = ? ";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            pst.setString(1, getGroup_name());
            
            rs = pst.executeQuery();
            if (rs.next()) {
                return rs.getString(1);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
        return "";
    }
    
    public boolean insert() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try{
            String sql = "insert into ipcc_extension_groups(group_,group_name,partner_id) values(?,?,?)";                             
            cn = AppServer.getConnection();
            group = Database.getSequenceValue(cn, "seq_extension");
            
            pst = cn.prepareStatement(sql);                        
            
            pst.setString(1, group);
            pst.setString(2, group_name);
            pst.setInt(3, partner_id.getPartner_id());
            
            return pst.executeUpdate() > 0;
        }
        finally{
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    public boolean edit() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try{
            String sql = "update ipcc_extension_groups "
                    + " set group_name = ?, partner_id = ?"
                    + " where group_ = ?";                             
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(sql);                        
                        
            pst.setString(1, group_name);
            pst.setInt(2, partner_id.getPartner_id());
            pst.setString(3, group);
            
            return pst.executeUpdate() > 0;
        }
        finally{
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    public boolean remove() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try{
            String sql = " delete from ipcc_extension_groups where group_ = ? ";                             
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(sql);
                        
            pst.setString(1, group);            
            
            return pst.executeUpdate() > 0;
        }
        finally{
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}
